package com.wuzhiming.istudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzhiming.istudy.pojo.Course;
import com.wuzhiming.istudy.pojo.CourseStage;
import com.wuzhiming.istudy.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: CourseStageController
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2022/3/30 15:46
 */
@Controller
public class CourseStageController extends BaseController{
    private Logger logger = LogUtils.getInstance(CourseStageController.class);

    /**
    * @Description: 根据课程类别id获得对应课程阶段
    * @Param: [id, from]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2022/3/30
    */
    @PostMapping("/getCourseStage")
    @ResponseBody
    public String getCourseStage(@RequestParam("courseTypeId")Integer courseTypeId){
        List<CourseStage> courseStage = courseStageService.queryAllById(courseTypeId);
        if(courseStage.size()>0){
            result.setCode(200);
        }else{
            result.setCode(100);
        }
        result.setData(courseStage);
        return JSONObject.toJSONString(result);
    }

    @PostMapping("/addStage")
    @ResponseBody
    public String addTag(CourseStage courseStage){
        List<CourseStage> courseStages = courseStageService.queryAll();
        for(int i=0;i<courseStages.size();i++){
            if(courseStages.get(i).getTitle().equals(courseStage.getTitle())){
                result.setCode(501);
                logger.warn("【失败】添加课程阶段，该名称已被占用");
                return JSONObject.toJSONString(result);
            }
        }
        courseStage.setStatus(1);
        courseStage.setAddTime(new Date());
        if(courseStageService.insert(courseStage)){
            result.setCode(200);
            logger.warn("【成功】添加标签");
        }else {
            result.setCode(500);
            logger.warn("【失败】添加标签");
        }
        return JSONObject.toJSONString(result);
    }

    @PostMapping("/updateStage")
    @ResponseBody
    public String updateStage(CourseStage courseStage){
        // 检查用户是不是没有更改任何的信息
        CourseStage old = courseStageService.queryById(courseStage.getId());
        if(old!=null){
            if (courseStage.getTitle().equals(old.getTitle())){
                result.setCode(501);
                logger.warn("【失败】更新课程阶段，没有修改任何信息");
                return JSONObject.toJSONString(result);
            }
        }
        // 如果更新之后的课程阶段名称已被占用
        List<CourseStage> courseStages = courseStageService.queryAll();
        for(int i=0;i<courseStages.size();i++){
            if(courseStages.get(i).getTitle().equals(courseStage.getTitle())){
                result.setCode(502);
                logger.warn("【失败】更新课程阶段，该名称已被占用");
                return JSONObject.toJSONString(result);
            }
        }
        if (courseStageService.update(courseStage)) {
            result.setCode(200);
            logger.warn("【成功】更新课程阶段");
        }else {
            result.setCode(500);
            logger.warn("【失败】更新课程阶段");
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: 删除课程阶段
     * @Param: [id]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/20
     */
    @GetMapping("/deleteStage")
    public String deleteTag(Integer id){
        if(id!=null){
            if(courseStageService.deleteById(id)){
                logger.warn("【成功】删除课程阶段");
            }else {
                logger.warn("【失败】删除课程阶段");
            }
        }
        return "redirect:/stages";
    }
}
